package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.core.util.UserUtil;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.downproductlog.IDownProLogService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/downproductlog/")
public class DownProductLogRest extends AbsRest{
	@Autowired
	IUserService userService;
	@Autowired
	IDownProLogService logService;

	@SuppressWarnings("static-access")
	@Path("findDownProductLog")
	@POST
	public String findDownProductLog(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "useruuid","start","pageSize");
		if(result == null){
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject userInfo = JsonUtils.converToJson(jwt.getSubject());
						if (userInfo != null) {
							JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
							Map<String, Object> parameters = MapUtil.getMap();
							User adminUser = userService.findByUserid(userInfo.getString(Config.tokenSignAttr));
							if (adminUser != null && UserUtil.checkRoleDown(adminUser)) {
									parameters.put(":useruuid", adminUser.getUseruuid());
							}

							if(!StringUtils.isBlank(json.getString("useruuid"))){
								parameters.put(":useruuid", json.getString("useruuid"));
							}

							if(!StringUtils.isBlank(json.getString("starttime"))){
								parameters.put(":starttime", json.getString("starttime"));
							}
							if(!StringUtils.isBlank(json.getString("endtime"))){
								parameters.put(":endtime", json.getString("endtime"));
							}

							return ResultMessage.getSucceed( JsonUtils.excludes(this.logService.findPageDownprolog(parameters, json.getInt("start"), json.getInt("pageSize")),
									new String[]{"","provincialInformation","operators","businessType","operators","status","fastchargetype",
											"","modifytime","downfailnum","downtimeinterva","","","auto","phonenum",
											"password","userstatus","usertype","errorCodesForUseruuid","expire","wechatid","lastlogintime","token"}));

						}
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}
